package org.misalen.web.database.model.mysql.pro;

import java.util.Arrays;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.misalen.annotations.Enumeration;
import org.misalen.annotations.ModelComment;
import org.misalen.web.database.model.mysql.BaseSqlModel;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * 网页广告载体
 * 
 * @author jianhun
 *
 */
@Entity
@Table(name = "ProWebAdvertisingCarrier")
@DynamicInsert(true)
@DynamicUpdate(true)
@JsonIgnoreProperties({ "webThumUrl" })
public class ProWebAdvertisingCarrier extends BaseSqlModel {

	@ModelComment("名称")
	@Column(name = "name", length = 100, nullable = false)
	private String name;

	@ModelComment("类型")
	@Enumeration("sys-carrier-type")
	@Column(name = "type", length = 100, nullable = false)
	private String type;

	@ModelComment("缩略图")
	@Column(name = "webThumUrl", length = 255, nullable = false)
	private String webThumUrl;

	@Transient
	@ModelComment("缩略图")
	private List<String> webThumUrls;

	@ModelComment("地址")
	@Column(name = "url", length = 100, nullable = false)
	private String url;

	public List<String> getWebThumUrls() {
		if(webThumUrl!=null){
			return Arrays.asList(webThumUrl.split(","));
		}else{
			return null;
		}
	}

	public void setWebThumUrls(List<String> webThumUrls) {
		this.webThumUrl = listToString(webThumUrls);
		this.webThumUrls = webThumUrls;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getWebThumUrl() {
		return webThumUrl;
	}

	public void setWebThumUrl(String webThumUrl) {
		if (webThumUrl != null) {
			this.webThumUrls = Arrays.asList(webThumUrl.split(","));
		} else {
			this.webThumUrls = null;
		}
		this.webThumUrl = webThumUrl;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public static String listToString(List<String> stringList) {
		if (stringList == null) {
			return null;
		}
		StringBuilder result = new StringBuilder();
		boolean flag = false;
		for (String string : stringList) {
			if (flag) {
				result.append(",");
			} else {
				flag = true;
			}
			result.append(string);
		}
		return result.toString();
	}
}
